package mylib.dir;

/**
 * Created with IntelliJ IDEA.
 * User: 1
 * Date: 02.08.12
 * Time: 20:48
 * To change this template use File | Settings | File Templates.
 */
public class GeometryUtils {
    public static final double EPS = 1e-9;

    public static double determinant(double a, double b, double c, double d) {
        return a * d - b * c;
    }

    public static long determinant(int a, int b, int c, int d) {
        return (long) a * d - (long) c * b;
    }

    /**
     * @param a сторона a
     * @param b сторона b
     * @param c сторона с
     * @return угол напротив стороны "c"
     */
    public static double getAngle(double a, double b, double c) throws TrianglePropertyDoesntHoldException {
        if (a + b + GeometryUtils.EPS < c || a + c + GeometryUtils.EPS < b || b + c + GeometryUtils.EPS < a)
            throw new TrianglePropertyDoesntHoldException();
        return Math.acos((a * a + b * b - c * c) / (2 * a * b));
    }


    public static class TrianglePropertyDoesntHoldException extends Throwable {
    }


    public static boolean isSegmentIntersect(Point a, Point b, Point c, Point d) {
        long v1 = Long.signum(a.subtract(b).multiply(d.subtract(a))) * Long.signum(a.subtract(b).multiply(c.subtract(a)));
        long v2 = Long.signum(d.subtract(c).multiply(a.subtract(d))) * Long.signum(d.subtract(c).multiply(b.subtract(d)));
        return v1 <= 0 && v2 <= 0 && intersect(a.x, b.x, c.x, d.x) && intersect(a.y, b.y, c.y, d.y);
    }

    private static boolean intersect(int x1, int x2, int x3, int x4) {
        return Math.max(x1, x2) >= Math.min(x3, x4) ||
                Math.max(x3, x4) >= Math.min(x1, x2);
    }
}
